const httpclient = require('../../utils/httpclient.js')

var app = getApp()
Page({
  data: {
    hidden: false,
    curNav: 1,
    curIndex: 0,
    cart: [],
    cartTotal: 0,
    cartPrice: 0,
    openCart: false,
    navList: [],
    dishesList: [],
    dishes: []
  },
  loadingChange() {
    setTimeout(() => {
      this.setData({
        hidden: true
      })
    }, 1000)
  },
  selectNav(event) {
    let id = event.target.dataset.id,
    index = parseInt(event.target.dataset.index);
    self = this;
    this.setData({
      curNav: id,
      curIndex: index
    })
  },
  // 添加商品
  selectDish(event) {
    let id = event.currentTarget.dataset.id;
    let price = event.currentTarget.dataset.price;
    let name = event.currentTarget.dataset.name;
    let flag = true;
    let cart = this.data.cart;
    let product = {};
    // 当前选择的菜单分类信息
    let dishesList = this.data.dishesList;
    dishesList.forEach(function (dishes, index) {
      dishes.forEach((dishe,s_index) => {
        if (dishe.id == id) {
          product = dishe;
          dishes[s_index].count = (dishe.count ? dishe.count : 0) + 1;
        }
      });
    });

    if (cart.length > 0) {
      cart.forEach(function (item, index) {
        if (item.id == id) {
          flag = false;
          cart[index].count = item.count + 1;
          // 给被选中的商品数加1
        }
      })
    }
    if (flag) cart.push(product);

    // 计算总的数量
    let cartTotal = 0;
    let cartTotalPrice = 0;
    cart.forEach(function (item, index) {
      cartTotal += item.count;
      cartTotalPrice += item.count * item.now_price;
    })
    this.setData({
      cartTotal: cartTotal,
      cartPrice: cartTotalPrice,
      cart: cart,
      dishesList: dishesList
    })
    if (cart.length <= 0){
      this.closeCart();
    }
  },
  // 删除一个商品
  // 添加商品
  removeDish(event) {
    let id = event.currentTarget.dataset.id;
    let price = event.currentTarget.dataset.price;
    let name = event.currentTarget.dataset.name;
    let flag = true;
    let cart = this.data.cart;
    
    // 当前选择的菜单分类信息
    let dishesList = this.data.dishesList;
    dishesList.forEach(function (dishes, index) {
      dishes.forEach((dishe, s_index) => {
        if (dishe.id == id) {
          dishes[s_index].count = (dishe.count ? dishe.count : 0) - 1;
        }
      });
    });

    if (cart.length > 0) {
      cart.forEach(function (item, index) {
        if (item.id == id) {
          cart[index].count = item.count - 1;
          // 如果减1后小于等于0将移除此商品
          if (cart[index].count<=0)
            cart.splice(index, 1);
        }
      })
    }

    // 计算总的数量
    let cartTotal = 0;
    let cartTotalPrice = 0;
    cart.forEach(function (item, index) {
      cartTotal += item.count;
      cartTotalPrice += item.count * item.now_price;
    })
    this.setData({
      cartTotal: cartTotal,
      cartPrice: cartTotalPrice,
      cart: cart,
      dishesList: dishesList
    })
    if (cart.length <= 0) {
      this.closeCart();
    }
  },
  clearCart(){
    // 当前选择的菜单分类信息
    let dishesList = this.data.dishesList;
    dishesList.forEach(function (dishes, index) {
      dishes.forEach((dishe, s_index) => {
        dishes[s_index].count = 0;
      });
    });
    this.setData({
      cart: [],
      dishesList: dishesList,
      cartTotal:0,
      cartPrice:0
    })
    this.closeCart();
  },
  openCart(){
    this.setData({
      openCart : true
    });
  },
  closeCart() {
    this.setData({
      openCart: false
    });
  },
  goPay() {
    app.globalData.cart = this.data.cart;
    wx.navigateTo({
      url: '/pages/to-pay-order/index'
    })
  },
  onLoad() {
    this.loadingChange();

    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    }); 

    httpclient.req(
      "/mall/find_product_cates",
      {
        app_id: app.globalData.appId
      },
      'get',
      function (res) {
        that.setData({
          navList: res.data
        });
        let cate_ids = '';
        for (var i = 0; i< res.data.length; i++){
          if(i==0){
            that.setData({
              curNav: res.data[i].id
            });
          }
          if (i != res.data.length-1){
            cate_ids += res.data[i].id + ',';
          }else{
            cate_ids += res.data[i].id;
          }
        }
        httpclient.req(
          "/mall/query_products_cateIds",
          {
            cate_ids: cate_ids,
            app_id: app.globalData.appId
          },
          'get',
          function (res) {
            that.setData({
              dishesList: res.data
            });
          },
          function (result) {
            console.log(result)
          }
        );
      },
      function (result) {
      }
    );
  }
})